﻿using Castle.DynamicProxy;
using CNKI.TPI.Web.Admin.IBLL;
using CNKI.TPI.Web.Admin.IDAO;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.IDAO;
using CNKI.TPI.Web.Search.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace CNKI.TPI.Web.Admin.BLL
{
    public class SubmitAdminService : BaseService, ISubmitAdminService
    {
        ISubmitDAO submitDao = null;
       IUserDBDAO userDBDao = null;
       public SubmitAdminService(ISubmitDAO submitDao, IUserDBDAO userDBDao)
        {
            this.submitDao = this.generator.CreateInterfaceProxyWithTarget<ISubmitDAO>(submitDao, new IInterceptor[] { new LogInterceptor() });
            this.userDBDao = this.generator.CreateInterfaceProxyWithTarget<IUserDBDAO>(userDBDao, new IInterceptor[] { new LogInterceptor() });
        }
        /// <summary>
        /// 保存SYS_CMS_OPTION表的数据
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool SaveOption(IList<SYS_CMS_OPTION> list)
        {
            //不管是更新还是新增都执行先删除后插入的规则
            bool result = false;
            try
            {
                submitDao.DeleteOption(1);//先删除再新增
                foreach (SYS_CMS_OPTION item in list)
                {
                    result = submitDao.InsertOption(item);
                    if (!result)
                    {
                        LogOpr.Error("表SYS_CMS_OPTION插入数据失败(数据库名)：" + item.DbName);
                    }
                }
            }

            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 删除_MetaData相关数据
        /// </summary>
        /// <param name="dbcode"></param>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public bool DelMetaData(string dbcode, int sysid)
        {
            bool result = false;
            try
            {
                result = submitDao.DeleteMetaData(dbcode, sysid);
                if (result) //主表删除成功,则删除相关表数据
                {
                    submitDao.DeleteMetaDataFile(dbcode, sysid);
                    submitDao.DeleteMetaDataRef(dbcode, sysid);
                }
            }

            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;

        }
        /// <summary>
        /// 获取不合格因素
        /// </summary>
        /// <param name="flage"></param>
        /// <returns></returns>
        public IList<SYS_CMS_ERRORINFOCONFIG> GetErrorInfo()
        {
            return submitDao.GetErrorInfo();
        }
        /// <summary>
        /// 更新MetaData数据信息
        /// </summary>
        /// <param name="dbcode"></param>
        /// <param name="markstate"></param>
        /// <param name="errorinfo"></param>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public bool UpdateMetaData(string dbcode, string markstate, string errorinfo, string sysid)
        {
            return submitDao.UpdateMetaData(dbcode, markstate, errorinfo, sysid);
        }
        /// <summary>
        /// 保存SYS_CMS_ERRORINFOCONFIG表的数据
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool SaveErrorList(IList<SYS_CMS_ERRORINFOCONFIG> list)
        {
            //不管是更新还是新增都执行先删除后插入的规则
            bool result = false;
            try
            {
                submitDao.DeleteErrorInfoAll();//先删除再新增  0删除所有
                foreach (SYS_CMS_ERRORINFOCONFIG item in list)
                {
                    result = submitDao.InsertErrorInfo(item);
                    if (!result)
                    {
                        LogOpr.Error("表SYS_CMS_ERRORINFOCONFIG插入数据失败：" + item.ErrorInfoList);
                    }
                }
            }

            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 获取数字对象使用字段
        /// </summary>
        /// <param name="flage"></param>
        /// <returns></returns>
        public IList<SYS_CMS_DATABASE_FIELD> GetNumField(string dbcode)
        {
            return submitDao.GetNumField(dbcode);
        }
        /// <summary>
        /// 更新SYS_CMS_DATABASE表的 papertype  PaperFieldValue DatetimeFlag 字段
        /// </summary>
        /// <param name="papertype"></param>
        /// <param name="field"></param>
        /// <param name="timecheck"></param>
        /// <param name="sysid"></param>
        /// <returns></returns>
        public bool UpdateCmsDataBase(string papertype, string field, string timecheck, string sysid)
        {
            return submitDao.UpdateCmsDataBase(papertype, field, timecheck, sysid);
        }
        /// <summary>
        /// 获取设置过提交样式的字段
        /// </summary>
        /// <param name="dbcode"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public IList<SYS_CMS_SUBMIT> IsEditStyle(string dbcode, string type)
        {
            return submitDao.IsEditStyle(dbcode, type);
        }
        /// <summary>
        /// 构造 下拉样式列表
        /// </summary>
        /// <returns></returns>
        public  IList<SubmitFieldType> GetSelectStyle()
        {
            IList<SubmitFieldType> result = new List<SubmitFieldType>();
            try
            {
                Type rightType = typeof(SubmitFieldType);
                foreach (int item in Enum.GetValues(rightType))
                {
                    result.Add((SubmitFieldType)item);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        public bool SaveStyle(IList<SYS_CMS_SUBMIT> list)
        {
            //不管是更新还是新增都执行先删除后插入的规则
            bool result = false;
            try
            {
                submitDao.DeleteStype(list[0].DBCODE,list[0].TYPE.ToString());//先删除再新增
                foreach (SYS_CMS_SUBMIT item in list)
                {
                    result = submitDao.InsertStype(item);
                    if (!result)
                    {
                        LogOpr.Error("表SYS_CMS_SUBMIT插入数据失败(字段名)：" + item.FIELDNAME);
                    }
                }
            }

            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 获取SYS_CMS_USER表字段
        /// </summary>
        /// <returns></returns>
        public List<string> GetUserColumnName()
        {
            List<string> listStr = new List<string>();
            DataTable dt = userDBDao.GetDataTableBySql("select top 1 * from SYS_CMS_USER");
            int columsNum = dt.Columns.Count;
            if (dt != null && columsNum > 0)
            {
                for (int i = 0; i < columsNum; i++)
                {
                    listStr.Add(dt.Columns[i].ColumnName);
                }
            }
            return listStr;
        }
        /// <summary>
        /// 删除提交样式
        /// </summary>
        /// <param name="dbcode"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public bool DelStyle(string dbcode, string type)
        {
            return submitDao.DeleteStype(dbcode,type);
        }
    }
}
